package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.dto.TrademarkDto;
import com.wizz.trade.information.dto.page.TrademarkPageDto;
import com.wizz.trade.information.entity.Trademark;
import com.wizz.trade.information.service.TrademarkService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.service.VarietyService;
import com.wizz.trade.information.vo.TrademarkVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资料模块/商品商标
 */
@RestController
@RequestMapping("/trademark")
public class TrademarkController {

    @Resource
    private TrademarkService trademarkService;

    @Resource
    private VarietyService varietyService;

    /**
     * 添加操作
     * @param trademarkDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult addTrademark(@RequestBody TrademarkDto trademarkDto) {
        Trademark trademark = new Trademark();
        BeanUtils.copyProperties(trademarkDto,trademark);
        trademarkService.addTrademark(trademark);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新（编辑）操作
     * @param trademarkDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult updateTrademark(@RequestBody TrademarkDto trademarkDto, @PathVariable("id") Integer id) {
        Trademark trademark = new Trademark();
        BeanUtils.copyProperties(trademarkDto,trademark);
        trademark.setId(id);
        trademarkService.updateTrademark(trademark);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }

    /**
     * 删除（撤销）操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult deleteTrademark(@PathVariable("id") Integer id) {
        trademarkService.deleteTrademark(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     * @param trademarkPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult<PageVo<TrademarkVo>> list(TrademarkPageDto trademarkPageDto){
        PageVo<Trademark> rawPage = trademarkService.getPage(trademarkPageDto);
        List<Trademark> data = rawPage.getData();
        List<TrademarkVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            TrademarkVo  trademarkVo = new TrademarkVo();
            BeanUtils.copyProperties(data.get(i), trademarkVo);
            String varietyName = varietyService.getNameById(data.get(i).getVariety());
            trademarkVo.setVariety(varietyName);
            dataVo.add(trademarkVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取商标下拉框
     * @param varietyId 品种id
     * @return 选项
     */
    @GetMapping("/options/{id}")
    public ResponseResult<List<Map<Integer, String>>> getOptions(@PathVariable("id") Integer varietyId){
        List<Map<Integer, String>> options = trademarkService.getOptions(varietyId);
        return new ResponseResult<>(StatusCode.OK, options);
    }
}
